
package moodish.client;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

/**
 * This class represents the Friends panel that is added to the ClientGUI.
 * 
 * @author es1-2013-group2
 *
 */
public class FriendsPanel extends JPanel{

	private JList friendsList;
	private FriendsListModel friendsListModel;
	private JButton unfriendButton;
	private ClientGUI clientGUI;
	private ArrayList<User> friends;

	/**
	 * The constructor creates a new FriendsPanel with the parameter ClientGUI.
	 * Sets the layout of this panel.
	 * It sets an actionListener to remove friends.
	 * The friends that are remove become users in the onlineUsersPanel.
	 * 
	 * @param clienteGUI ClientGUI.
	 */
	public FriendsPanel(final ClientGUI clienteGUI){
		this.clientGUI = clienteGUI;
		this.friends = new ArrayList<User>();
		friendsListModel = new FriendsListModel(friends);
		friendsList = new JList(friendsListModel);
		friendsListModel.sortList();
		friendsList.updateUI();
		friendsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		friendsList.setSelectedIndex(0);
		friendsList.setCellRenderer(new FriendsCellRenderer());
		friendsList.setVisibleRowCount(5);
		User protoCellValue = new User("asdasdasfasgsdfgdsf", Mood.Confused);
		friendsList.setPrototypeCellValue(protoCellValue);
		JScrollPane listScrollPane = new JScrollPane(friendsList);

		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(listScrollPane);
		this.setBorder(BorderFactory.createTitledBorder("Friends"));

		unfriendButton = new JButton("Unfriend");
		unfriendButton.setEnabled(false);
		unfriendButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int index = friendsList.getSelectedIndex();

				User user = null;
				for(User friend: friends){
					if(friend.getNickname() == ((User) friendsListModel.getElementAt(index)).getNickname()){
						user = friend;
					}
				}
				clienteGUI.removeFriend(user);
				clienteGUI.addOnlineUser(user);

				//clientComm.unfriendship((String) friendsList.getSelectedValue().getName);
				friendsListModel.remove(index);
				friendsList.updateUI();

				int size = friendsListModel.getSize();

				if (size == 0) { //Nobody's left, disable unfriend.
					unfriendButton.setEnabled(false);

				} else { //Select an index.
					if (index == friendsListModel.getSize()) {
						//removed item in last position
						index--;
					}

					friendsList.setSelectedIndex(index);
					friendsList.ensureIndexIsVisible(index);
				}
			}
		});

		this.add(unfriendButton);
	}

	/**
	 * Add a new friend (User) to the list.
	 * Sort the list according to friends mood.
	 * 
	 * @param friend User that will be added to the list.
	 */
	public void addFriend(User friend) {
		friends.add(friend);
		friendsListModel.sortList();
		friendsList.updateUI();
		if(!unfriendButton.isEnabled())
			unfriendButton.setEnabled(true);
		friendsList.setSelectedIndex(0);
	}

	public void removeFriend(String nickname) {

		User user = null;
		for(User friend: friends){
			if(friend.getNickname().equals(nickname))
				user = friend;
		}


		friends.remove(user);
		friendsListModel.sortList();
		friendsList.updateUI();
		friendsList.setSelectedIndex(0);

		if(friends.isEmpty())
			unfriendButton.setEnabled(false);

	}

	/**
	 * 
	 * Remove all friends from the friends list.
	 * 
	 */
	public void removeAllFriends(){
		friends.clear();
		friendsList.updateUI();
		unfriendButton.setEnabled(false);
	}

	public void setFriendMood(String friendNickname, String mood){

		for(User friend: friends){
			if(friend.getNickname().equals(friendNickname))
				friend.setMood(Mood.valueOf(mood));
		}
		friendsListModel.sortList();
		friendsList.updateUI();
	}

	/**
	 * Inner Class that identifies the components to paint the cells in a JList: text, iconImage.
	 * It also sets the behavior of the selected cell .
	 * 
	 * @author es1-2013-group2
	 *
	 */
	protected class FriendsCellRenderer extends JLabel implements ListCellRenderer<User> {

		@Override
		public Component getListCellRendererComponent(
				JList<? extends User> list, User friend, int index,
				boolean isSelected, boolean cellHasFocus) {


			setText(friend.getNickname());
			setIcon(friend.getImageIcon());

			if (isSelected) {
				setBackground(list.getSelectionBackground());
				setForeground(list.getSelectionForeground());
			} else {
				setBackground(list.getBackground());
				setForeground(list.getForeground());
			}
			setEnabled(list.isEnabled());
			setFont(list.getFont());
			setOpaque(true);
			return this;
		}
	}

	/**
	 * 
	 * Inner Class that provides a List with its contents. 
	 * It allows to sort the elements in the List.
	 * 
	 * @author es1-2013-group2
	 *
	 */
	protected class FriendsListModel extends AbstractListModel{

		ArrayList<User> friends;

		public FriendsListModel(ArrayList<User> friends){
			this.friends = friends;
		}

		@Override
		public Object getElementAt(int index) {
			return (User)friends.get(index);
		}

		@Override
		public int getSize() {
			return friends.size();
		}

		public ArrayList<User> getFriedsList(){
			return friends;
		}

		public void setList(ArrayList<User> array){
			this.friends = array;
		}

		public void sortList(){
			Collections.sort(friends);
		}

		public void remove(int index){
			friends.remove(index);
			fireContentsChanged(this, 0, getSize());
		}




	}








}